package com.xumz.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.xumz.model.ClassName;
import com.xumz.model.Student;
import com.xumz.model.StudentManage;
import com.xumz.service.ClassService;
import com.xumz.service.StudentService;

@Controller
public class ClassgroupController {
	@Autowired
	ClassService cms;
	@Autowired
	StudentService studentService;
	
	public ClassgroupController() {
		System.out.println("ClassgroupController");
	}
	
	@RequestMapping(value = "/classinformation/{classid}",method=RequestMethod.GET)
	public String stuinformation(Model model,@PathVariable("classid") String classid) throws UnsupportedEncodingException{
		ClassName classname = cms.findClassNameById(classid);
		List<Student> list = studentService.selectStudentByClassId(classid);
		model.addAttribute("classname", classname);
		model.addAttribute("number",list.size());
		model.addAttribute("list",list);
		return "teacher/admin/classgroup/information";
	}
	
	@RequestMapping("classgroupUpdate")
	public String classgroupUpdate(HttpServletRequest request, HttpServletResponse response,Model model, 
			@RequestParam("classname")String classname, @RequestParam("classid")String classid){
		String name=(String) request.getSession().getAttribute("name");
		if (classname.equals("")) {
			JOptionPane.showMessageDialog(null, "不能为空"); 
			List<ClassName> list = cms.selectClassNameByTeacherName(name);
			model.addAttribute("list",list);
			return "teacher/admin/student/index";
		}
		ClassName className = new ClassName(classid, classname);
		if (cms.classgroupUpdate(className) == true) {
			request.setAttribute("classgroupupdate", "1");
//			JOptionPane.showMessageDialog(null, "修改完毕"); 
		} else {
//			JOptionPane.showMessageDialog(null, "修改失败"); 
		}
		List<ClassName> list = cms.selectClassNameByTeacherName(name);
		model.addAttribute("list",list);
		return "teacher/admin/student/index";
	}
	@RequestMapping("classgroupDelete")
	public String classgroupDelete(HttpServletRequest request, HttpServletResponse response,Model model,
			@RequestParam("classid")String classid){
		if (cms.classgroupDelete(classid) == true) {
			request.setAttribute("classgroupdelete", "1");
//			JOptionPane.showMessageDialog(null, "班级已成功删除"); 
		} else {
//			JOptionPane.showMessageDialog(null, "删除失败"); 
		}
		String name=(String) request.getSession().getAttribute("name");
		List<ClassName> list =  cms.selectClassNameByTeacherName(name);
		model.addAttribute("list",list);
		return "teacher/admin/student/index";
	}
	@RequestMapping("classgroupIndex")
	public String login(HttpServletRequest request, HttpServletResponse response,Model model){	//班级管理
		String name=(String) request.getSession().getAttribute("name");
		List<ClassName> classNameList = cms.selectClassNameByTeacherName(name);
		model.addAttribute("classNameList",classNameList);
		List<StudentManage> list = cms.selectAllStudentByTeacherName(name);
		model.addAttribute("list",list);
		
		return "teacher/admin/classgroup/index";
	}
	@RequestMapping("classcontrol")
	public String classcontrol(HttpServletRequest request, HttpServletResponse response,Model model){
		String name=(String) request.getSession().getAttribute("name");
		List<ClassName> list = cms.selectClassNameByTeacherName(name);
		model.addAttribute("list",list);
		return "teacher/admin/student/index";
	}
	@RequestMapping("/classgroupAdd")
	public String classgroupAdd(Model model){
		return "teacher/admin/classgroup/add";
	}
	@RequestMapping("/classgroupAddup")
	public String classgroupAddup(HttpServletRequest request, HttpServletResponse response,	//创建班级
			Model model, @RequestParam("classname")String classname, @RequestParam("classdescribe")String classdescribe){
		if (classname.equals("")) {
			request.setAttribute("classgroupaddup", "1");
//			JOptionPane.showMessageDialog(null, "不能为空"); 
			return "teacher/admin/classgroup/add";
		}
		HttpSession session=request.getSession();
		String name=(String) session.getAttribute("name");
		if (cms.classgroupAdd(classname, classdescribe,name)){
			request.setAttribute("classgroupaddup", "2");
//			JOptionPane.showMessageDialog(null, "班级" + classname + "创建成功"); 
		} else {
//			JOptionPane.showMessageDialog(null, "创建班级失败"); 
		}
		List<ClassName> list = cms.selectClassNameByTeacherName(name);
		model.addAttribute("list",list);
		return "teacher/admin/student/index";
	}
	@RequestMapping("/studentAdd")
	public String studententry(HttpServletRequest request, HttpServletResponse response,Model model){		//成员录入
		String name=(String) request.getSession().getAttribute("name");
		List<ClassName> listClass = cms.selectClassNameByTeacherName(name);
		model.addAttribute("listClass",listClass);
		return "teacher/admin/student/add";
	}
}
